//
//  Utils.m
//  CarTown
//
//  Created by xionghao on 15/8/11.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "NetWorkUtils.h"
#import "Token.h"
#import <AFNetworking.h>


#import "IWUploadParam.h"
@implementation NetWorkUtils
+(void)getToken:(void (^)(NSString *tokenstr))tokenresponse{
    Token *token=[Token getToken];
    BOOL isold=NO;
    if (token!=nil) {
        NSDate *date=[NSDate date];
        NSTimeInterval secondsInterval= [date timeIntervalSinceDate:token.time];
        isold=(secondsInterval>3500*2);
        
    }
    if (token==nil||isold) {
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        long long int date = (long long int)time;
        NSLog(@"date\n%lld", date); //1295322949
        NSString *tokenstr=[NSString stringWithFormat:@"%@%lld",APPkey,date];
        NSString *sha1=[tokenstr getSha1String:tokenstr];
        NSLog(@"%@",sha1);
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"make"]=sha1;
        params[@"time"]=[NSString stringWithFormat:@"%lld",date];

        [NetWorkUtils post:tokenurl parameters:params success:^(id responseObject) {
            
            NSDictionary *dict=responseObject;
            
            NSNumber *state=dict[@"state"];
            if ([state intValue]==200) {
               Token *token=[Token TokenWithDict:dict[@"msg"]];
                
                if (tokenresponse) {
                    tokenresponse(token.token);
                }
            }

        } failure:^(NSError *error) {
            if (tokenresponse) {
                tokenresponse(@"");
            }
            NSLog(@"网络错误,获取token失败");
        }];
        
    }else{
        if (tokenresponse) {
            tokenresponse(token.token);
        }
    }

}

+ (void)get:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 获取http请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    
    // 发送请求
    [mgr GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)post:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 获取请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    AFJSONResponseSerializer *serializer=[AFJSONResponseSerializer serializer];
    serializer.removesKeysWithNullValues=YES;
//    AFHTTPResponseSerializer *serializer=[AFHTTPResponseSerializer serializer];
    mgr.responseSerializer=serializer;
    
    // 发送请求
    [mgr POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSData *data=responseObject;
//        NSString *string=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        if (success) {
//            NSLog(@"%@",string);
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

+ (void)upload:(NSString *)URLString parameters:(id)parameters uploadParam:(NSArray *)uploadParamArray success:(void (^)(id responseObject))success failure:(void (^)(NSError *))failure
{
    // 创建请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 发送请求
    [mgr POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        for (int i=0; i<uploadParamArray.count; i++) {
            
            IWUploadParam *uploadParam=uploadParamArray[i];
            [formData appendPartWithFileData:uploadParam.data name:uploadParam.paramName fileName:uploadParam.fileName mimeType:uploadParam.mineType];
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}

@end
